package com.hue.gulimail.gulimailcoupon.service.impl;

import com.hue.gulimail.gulimailcoupon.entity.SeckillSkuRelationEntity;
import com.hue.gulimail.gulimailcoupon.service.SeckillSkuRelationService;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hue.common.utils.PageUtils;
import com.hue.common.utils.Query;
import com.hue.gulimail.gulimailcoupon.dao.SeckillSessionDao;
import com.hue.gulimail.gulimailcoupon.entity.SeckillSessionEntity;
import com.hue.gulimail.gulimailcoupon.service.SeckillSessionService;

/**
 * @author admin
 */
@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {
    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getSeckill3Days() {
        //查询最近3天的秒杀活动
        QueryWrapper<SeckillSessionEntity> wrapper = new QueryWrapper<>();
        wrapper.between("start_time", startTime(), endTime());
        wrapper.eq("publish", 0);
        List<SeckillSessionEntity> sessions = list(wrapper);

        if (sessions != null && sessions.size() > 0) {
            //查询每个活动对应的商品信息
            for (SeckillSessionEntity session : sessions) {
                List<SeckillSkuRelationEntity> relations = seckillSkuRelationService.getRelationBySessionId(session.getId());
                session.setSkuRelations(relations);
            }
        }
        return sessions;
    }

    @Override
    public void setSessionPublish(List<Long> ids) {
        List<SeckillSessionEntity> sessions = ids.stream().map(id -> {
            SeckillSessionEntity session = new SeckillSessionEntity();
            session.setId(id);
            session.setPublish(1);
            return session;
        }).collect(Collectors.toList());

        updateBatchById(sessions);
    }

    /**
     * 获取当天的开始时间
     *
     * @return
     */
    private String startTime() {
        LocalDate now = LocalDate.now().plusDays(-1);

        LocalTime min = LocalTime.MIN;
        String startTime = LocalDateTime.of(now, min).format(DateTimeFormatter.ofPattern("yyyy-MM-dd kk:mm:ss"));

        return startTime;
    }

    /**
     * 获取当天2天后的结束时间
     *
     * @return
     */
    private String endTime() {
        LocalDate now = LocalDate.now();

        //获取当天时间2天后的时间
        LocalDate now2Day = now.plusDays(2);

        LocalTime max = LocalTime.MAX;
        String endTime = LocalDateTime.of(now2Day, max).format(DateTimeFormatter.ofPattern("yyyy-MM-dd kk:mm:ss"));

        return endTime;
    }
}
